package com.hhxy.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @Author MYH
 * @Date 2022/2/23 14:45
 * @Description
 **/
@Component
public class TableTimeMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        //填充创建时间字段
        Object created = getFieldValByName("createTime", metaObject);
        if (Objects.isNull(created)){
            setFieldValByName("createTime", new Date(), metaObject);
        }

        //填充更新时间字段
        Object updated = getFieldValByName("updateTime", metaObject);
        if (Objects.isNull(updated)){
            setFieldValByName("updateTime", new Date(), metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新数据时直接更新字段
        setFieldValByName("updateTime", new Date(), metaObject);
    }
}
